package org.p2psimulator.entity.protocol.messages.gnutella.xrep;

import java.util.HashMap;

import org.p2psimulator.entity.Id;
import org.p2psimulator.entity.Message;
import org.p2psimulator.entity.behavior.BehaviorTypes;
import org.p2psimulator.entity.protocol.messages.MessageTypes;
import org.p2psimulator.util.Tuple;

@SuppressWarnings ("unchecked")
public class XrepPollReply  extends Message{




	public XrepPollReply(Id origin, Id destination, int transactionId,
			BehaviorTypes originBehavior) {
		super(origin, destination, transactionId, originBehavior);

		this.serventVotes = new HashMap<Id, Double>();
		this.resourceVotes = new HashMap<Id, Double>();

	}
	private Id serventId;

	private Id pollReplyId;

	private HashMap<Id, Double> resourceVotes;

	private HashMap<Id, Double> serventVotes;

	public Tuple<Id, Double> getBestServentVote(){
		double max = Integer.MIN_VALUE;
		Id maxId = null;

		for(Id key : this.serventVotes.keySet()){

			double v = this.serventVotes.get(key);
			if (v > max){
				max = v;
				maxId = key;
			}
		}
		if (maxId != null){	return new Tuple<Id, Double>(maxId, max); }
		else{ return null;	}
	}

	public Tuple<Id, Double> getBestResourceVote(){
		double max = Integer.MIN_VALUE;
		Id maxId = null;

		for(Id key : this.resourceVotes.keySet()){

			double v = this.resourceVotes.get(key);
			if (v > max){
				max = v;
				maxId = key;
			}
		}
		return new Tuple<Id, Double>(maxId, max);
	}


	public Double getResourceVote(Id idResource){
		return this.resourceVotes.get(idResource);
	}
	public Double getServentVote (Id idServent){
		return this.serventVotes.get(idServent);
	}
	public Id getServentId() {
		return serventId;
	}
	public void setServentId(Id serventId) {
		this.serventId = serventId;
	}
	public HashMap<Id, Double> getResourceVotes() {
		return resourceVotes;
	}
	public void setResourceVotes(HashMap<Id, Double> resourceVote) {
		this.resourceVotes = resourceVote;
	}
	public HashMap<Id, Double> getServentVotes() {
		return serventVotes;
	}
	public void setServentVotes(HashMap<Id, Double> serventVote) {
		this.serventVotes = serventVote;
	}
	public Id getPollReplyId() {
		return pollReplyId;
	}
	public void setPollReplyId(Id pollReplyId) {
		this.pollReplyId = pollReplyId;
	}

	public String getName(){
		return MessageTypes.XREP_POLLREPLY.getName();
	}
	@Override
	public MessageTypes getMessageType() {
		return MessageTypes.XREP_POLLREPLY;
	}
}
